<template>
<page title="用餐结算表明细" has-back>
  <food-needs-form v-model="form.foodNeed"></food-needs-form>
  <el-row justify="center">
    <el-col :span="15">
      <h1>实际用餐情况</h1>
      <el-form :model="form.foodCheck" label-width="100px" label-position="right">
        <el-form-item label="用餐情况">
          <el-table :data="form.actualList" border header-cell-class-name="table-header" empty-text="无用餐情况">
            <el-table-column
                prop="date"
                :resizable="false"
                header-align="center"
                align="center"
                label="日期">
              <template #default="scope">
                <edit-text v-model="scope.row.date"></edit-text>
              </template>
            </el-table-column>
            <el-table-column
                prop="dining"
                :resizable="false"
                header-align="center"
                align="center"
                label="就餐情况">
              <template #default="scope">
                <edit-text v-model="scope.row.detail"></edit-text>
              </template>
            </el-table-column>
            <el-table-column
                prop="detail"
                :resizable="false"
                header-align="center"
                align="center"
                label="备注 ">
              <template #default="scope">
                <edit-text v-model="scope.row.other"></edit-text>
              </template>
            </el-table-column>
            <el-table-column
                :resizable="false"
                header-align="center"
                align="center"
                label="操作">
              <template #default="scope">
                <el-button
                    type="text"
                    @click="handleDelete(scope.$index, scope.row)" style="color: red">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="text" @click="addFoodNeed">新增</el-button>
        </el-form-item>
        <el-form-item label="用餐标准">
          <el-row>
            <el-col :span="12">
              <el-input v-model="form.foodCheck.standard" type="textarea" placeholder="请填写用餐标准" rows="5"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="金额">
          <el-row>
            <el-col :span="12">
              <el-input v-model="form.foodCheck.price" type="number" placeholder="请填写用餐金额">
                <template #suffix>
                  元
                </template>
              </el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-row justify="center">
          <el-button type="primary" plain @click="save">保 存</el-button>
        </el-row>
      </el-form>
    </el-col>
  </el-row>
</page>
</template>

<script>
import Page from "@/components/Page";
import FoodNeedsForm from "@/components/FoodNeedsForm";
import EditText from "@/components/EditText";
import axios from "axios";
// import moment from "moment";
export default {
  name: "FoodCheckInfo",
  components: {EditText, FoodNeedsForm, Page},
  data(){
    return{
      form:{
        foodNeed:{
          detailList:[],
          details:"",
        },
        foodCheck: {},
        actualList: []
      }
    }
  },
  methods:{
    findByProjectId(id){
      return axios.get(this.$host+"/check/food/"+id).then((response)=>{
        if(response.data.code === 0){
          this.form = response.data.data;
          // this.form.accCheck.date = moment(this.form.accCheck.date).format("YYYY年MM月")
          // console.log(this.form)
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    updateFoodInfo(data){
      return axios.put(this.$host+"/check/food/",data).then(response=>{
        if(response.data.code === 0){
          this.$message.success("修改成功");
          this.$router.replace("checkReport");
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    addFoodNeed(){
      this.form.actualList.push({
        date:'',
        standard:'',
        detail:'',
        foodCheckId: this.form.foodCheck.id
      })
    },
    handleDelete(index){
      this.form.actualList.splice(index,1)
    },
    save(){
      this.updateFoodInfo(this.form).then(()=>this.findByProjectId(this.$router.currentRoute.value.query.id));
    }
  },
  created() {
    this.findByProjectId(this.$router.currentRoute.value.query.id);
  }
}
</script>

<style scoped>

</style>